{% set user = session_user () %}

{# Number of new messages #}
{% if user %}
    {% set unread_messages = new_messages (user.id) %}
{% endif %}

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        
        <link href="/css/freepost.css" rel="stylesheet">
        
        <title>{{ title ~ ' - ' if title else '' }}freepost</title>
    </head>
    
    <body>
        <div class="container">
        
            <div class="header">
                
                <div class="title-small">
                    
                    
                    {% if topic %}
                        <a href="{{ url ('topic', name=topic) }}" class="flex-item logo">
                            topic / {{ topic }}
                        </a>
                    {% else %}
                        <a href="{{ url ('homepage') }}{{ '?sort=new' if user and user.preferred_feed == 'new' }}" class="flex-item logo">
                            free
                            <img alt="🐵&nbsp;" title="freepost" src="/images/freepost.svg" class="monkey" />
                            post
                        </a>
                    {% endif %}
                    
                    <div class="burger-icon {{ 'notify' if unread_messages }}">
                        <div class="line1"></div>
                        <div class="line2"></div>
                        <div class="line3"></div>
                    </div>
                </div>
                
                <div class="title-large">
                    {% if topic %}
                        <a href="{{ url ('topic', name=topic) }}" class="flex-item logo">
                            topic / {{ topic }}
                        </a>
                    {% else %}
                        <a href="{{ url ('homepage') }}{{ '?sort=new' if user and user.preferred_feed == 'new' }}" class="flex-item logo">
                            freepost
                        </a>
                    {% endif %}
                    
                    <a href="{{ url ('homepage') }}{{ '?sort=new' if user and user.preferred_feed == 'new' }}" class="flex-item logo">    
                        <img alt="🐵&nbsp;" title="freepost" src="/images/freepost.svg" class="monkey" />
                    </a>
                </div>
                
                <div class="menu" id="menu">
                    <a href="{{ url ('topic', name=topic) if topic else url ('homepage') }}" class="flex-item {{ 'active_page' if active_page == 'hot' }}">
                        Hot
                    </a>
                    <a href="{{ url ('topic', name=topic) if topic else url ('homepage') }}?sort=new" class="flex-item {{ 'active_page' if active_page == 'new' }}">
                        New
                    </a>
                    
                    {% if user %}
                        <a href="{{ url ('submit') }}{{ '?topic=' ~ topic if topic }}" class="flex-item {{ 'active_page' if active_page == 'submit' }}">
                            Submit
                        </a>
                    {% endif %}
                    
                    <a href="{{ url ('search') }}" class="flex-item {{ 'active_page' if active_page == 'search' }}">Search</a>
                    
                    <a href="{{ url ('about') }}" class="flex-item {{ 'active_page' if active_page == 'about' }}">About</a>
                    
                    {% if user %}
                        {% if unread_messages %}
                            <a href="{{ url ('user_replies') }}" class="new_messages flex-item">
                                {{ user.username }} ({{ unread_messages }})
                            </a>
                        {% else %}
                            <a href="{{ url ('user_settings') }}" class="flex-item  {{ 'active_page' if active_page == 'user' }}">
                                {{ user.username }}
                            </a>
                        {% endif %}
                    
                        <a href="{{ url ('logout') }}" class="flex-item">Log out</a>
                    {% else %}
                        <a href="{{ url ('login') }}" class="flex-item {{ 'active_page' if active_page == 'login' }}">Log in</a>
                    {% endif %}
                </div>
            </div>
            
            <div class="content">
                {% block content %}{% endblock %}
            </div>

            <footer>
                <ul>
                    <li>
                        <img alt="RSS" title="" src="/images/rss.png" />
                        <a href="/rss/hot">Hot</a> •
                        <a href="/rss/new">New</a> •
                        <a href="/rss_comments">Comments</a>
                    </li>
                    <li>
                        <img alt="Source" title="" src="/images/source.png" />
                        <a href="https://notabug.org/zPlus/freepost">Source code</a>
                    </li>
                    <li>
                        <img alt="Peers" title="" src="/images/peers.png" />
                        <a href="http://peers.community">Peers</a>
                        <a href="https://dokk.org/The_Peers_Community">Community</a>
                    </li>
                    <li>
                        <img alt="TuxFamily" title="" src="/images/tuxfamily.png" />
                        Hosted at <a href="https://dokk.org/TuxFamily">TuxFamily</a>
                    </li>
                </ul>
            </footer>
        </div>

        {# When users vote, this <iframe/> is used as target, such that
         # the page is not reloaded
         #}
        <iframe name="vote_sink" class="vote_sink"></iframe>

        <script src="/javascript/freepost.js"></script>
    </body>
</html>
